package cn.com.dev;

import cn.com.elite.Util;
import cn.com.kh.KHUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * @Title: cn.com.dev-api5GDev
 * @Description:ngsapitask-sw 系统API接口开发Demo，重点关注业务逻辑部分
 * @Author: lijun
 * @Date 2022-06-22 16:07
 */
public class api5GDev {
    private static final Log log = LogFactory.getLog(api5GDev.class);
    private static String access_token = "";
    private static String serverRoot = "http://h5.sz-water.com.cn";
    private static String appId = "szswjt";// Client ID
    private static String appSecret = "PLDJYc8uRJ6UGZlE";// 密钥

    private static String getTokenURL = serverRoot + "/api/auth/v1/authentication/token";
    private static String getSMSListURL = serverRoot + "/api/open-api/v1/mode/vpvsms/list";
    private static String getSendSMSURL = serverRoot + "/api/open-api/v2/vpvsms/sendMmsMsg";
    private static String sendTrigger5Gmsg_URL = serverRoot + "/api/open-api/v1/msg/sendTrigger5Gmsg";
    private static String sendDynamicTrigger5Gmsg_URL = serverRoot + "/api/open-api/v1/msg/sendDynamicTrigger5Gmsg";

    public static void main(String[] args) throws Exception {

        // 1-获取Token
        access_token = KHUtil.isNull(access_token) ? get5GToken(getTokenURL, appId, appSecret) : access_token;

        // 2-获取短信模板-5G多媒体短信模板列表查询
        // String smsList = get5GSMSList(getSMSListURL, access_token, appId, appSecret);
        // System.err.println("获取短信模板列表:" + smsList);

        // 3-5G多媒体短信发送
        // String smsSend = get5GSendSMS(getSendSMSURL, access_token);
        // System.err.println("短信发送:" + smsSend);

        // 4-5G发送-无参数
        //String smsSendP = sendTrigger5Gmsg(sendTrigger5Gmsg_URL, access_token, "85453cf7ed67917ecdab5cb79af43491", "471", "18675571253");
        //System.err.println("5G发送-无参数:" + smsSendP);

        // 5-5G发送-有参数
        String smsSendNP = sendDynamicTrigger5Gmsg(sendDynamicTrigger5Gmsg_URL, access_token, "9e8687dea448d59441bc54ddb4ab35fa", "471", "13267067312");
        System.err.println("5G发送-有参数:" + smsSendNP);

    }

    public static String get5GToken(String url, String appId, String appSecret) throws Exception {

        // 查出传真发送的地址
        Map<String, String> headParams = new HashMap<String, String>();
        headParams.put("Content-Type", "application/json");

        JSONObject postJson = new JSONObject();
        postJson.put("client_id", appId);
        postJson.put("client_secret", appSecret);
        postJson.put("grant_type", "client_credentials");
        log.debug("5G获取token接口-调用地址:" + url + " 调用参数信息:" + postJson);

        String resultInfo = Util.doHttpPostUseHead(url, postJson.toString(), "UTF-8", 120000, 0, headParams);
        log.debug("5G获取token接口-调用返回:" + resultInfo);

        JSONObject resultJson = JSONObject.fromObject(resultInfo);
        String access_token = resultJson.getJSONObject("data").getString("access_token");
        String expires_in = resultJson.getJSONObject("data").getString("expires_in");

        return access_token;
    }

    public static String get5GSMSList(String url, String access_token, String appId, String appSecret) throws Exception {

        // 查出传真发送的地址
        Map<String, String> headParams = new HashMap<String, String>();
        headParams.put("Content-Type", "application/json");
        headParams.put("Authorization", "Bearer " + access_token);
        headParams.put("serverRoot", "http://121.37.214.141/sendVpMsg");

        String resultInfo = Util.doHttpGetUseHead(url + "?pageNum=1&pageSize=1000&name", "UTF-8", 120000, 0, headParams);
        log.debug("返回请求体：" + resultInfo);

        JSONObject resultJson = JSONObject.fromObject(resultInfo);
        JSONArray resultArr = resultJson.getJSONObject("data").getJSONArray("records");
        for (int i = 0; i < resultArr.size(); i++) {
            String id = resultArr.getJSONObject(i).getString("id");
            String name = resultArr.getJSONObject(i).getString("name");
            String sign = resultArr.getJSONObject(i).getString("sign");
            String creator = resultArr.getJSONObject(i).getString("creator");
            String createDate = resultArr.getJSONObject(i).getString("createDate");
            String modifier = resultArr.getJSONObject(i).getString("modifier");
            String modifyDate = resultArr.getJSONObject(i).getString("modifyDate");
            String delFlag = resultArr.getJSONObject(i).getString("delFlag");
            String dynamicValues = resultArr.getJSONObject(i).getString("dynamicValues");

            String vpStatus = resultArr.getJSONObject(i).getString("vpStatus");
            String ydStatus = resultArr.getJSONObject(i).getString("ydStatus");
            String ltStatus = resultArr.getJSONObject(i).getString("ltStatus");
            String dxStatus = resultArr.getJSONObject(i).getString("dxStatus");
            String auditStatus = vpStatus + "/" + ydStatus + "/" + ltStatus + "/" + dxStatus;

            System.out.println("处理模板auditStatus:" + auditStatus);
            String mmsbody = resultArr.getJSONObject(i).getJSONObject("content").getString("subject");

            JSONArray mmsbodyArr = resultArr.getJSONObject(i).getJSONObject("content").getJSONArray("mmsbody");
            for (int j = 0; j < mmsbodyArr.size(); j++) {
                mmsbody = mmsbody + mmsbodyArr.getJSONObject(j).getString("body");
            }
            System.err.println("处理模板auditStatus:" + auditStatus + "处理模板ID:" + id + " 模板名称:" + name + System.getProperty("line.separator") + "--内容:" + mmsbody);

            /*String dealSQL = "select * from DocTemplate_5G where Doctemplate_Id = ?";
            List<Map<String, Object>> selectCus = jt.queryForList(dealSQL, id);

            log.debug("selectCus：" + selectCus);
            log.debug("检查数据(" + id + ") 数量：" + selectCus.size());

            if (selectCus.size() == 0) {
                String insertSQL = "Insert Into DocTemplate_5G (Doctemplate_Id, Docgroup_Id, Description, Documentpath, Documentpath2, Status, Sms_Sign, Doctype, auditStatus, DynamicValues, Isparameter, Createrby, Creatertime, Modifiedby, Modifiedtime) Values (?, null, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
                int num = jt.update(insertSQL, id, name, mmsbody, mmsbodyArr.toString(), delFlag, sign, "5", auditStatus, dynamicValues, mmsbodyArr.size(), creator, createDate, modifier, modifyDate);
                log.debug("新增数据(" + id + ") 数量：" + num);
            } else {
                String updateSQL = "Update DocTemplate_5G Set Description = ?, Documentpath = ?, Documentpath2 = ?, Status = ?, Sms_Sign = ?, auditStatus = ?, Modifiedby = 'SELITE', Modifiedtime = Now() Where Doctemplate_Id = ?";
                int num = jt.update(updateSQL, name, mmsbody, mmsbodyArr.toString(), delFlag, sign, auditStatus);
                log.debug("更新数据(" + id + ") 数量：" + num);
            }*/

        }

        return resultInfo;
    }

    public static String get5GSendSMS(String url, String access_token) throws Exception {

        // 查出传真发送的地址
        Map<String, String> headParams = new HashMap<String, String>();
        headParams.put("Content-Type", "application/json");
        headParams.put("Authorization", "Bearer " + access_token);
        headParams.put("serverRoot", "http://121.37.214.141/sendVpMsg");

        JSONObject postJson = new JSONObject();
        postJson.put("templateId", "1009154042035933184");

        JSONArray phonesList = new JSONArray();
        JSONObject phones = new JSONObject();
        //JSONArray paramsList = new JSONArray();
        JSONArray paramsList = JSONArray.fromObject("[{\"paramName\":\"param1\",\"paramValue\":\"param1\"},{\"paramName\":\"param2\",\"paramValue\":\"param2\"},{\"paramName\":\"param3\",\"paramValue\":\"param3\"},{\"paramName\":\"param4\",\"paramValue\":\"param4\"},{\"paramName\":\"param5\",\"paramValue\":\"param5\"},{\"paramName\":\"param6\",\"paramValue\":\"param6\"}]");

        System.err.println(paramsList);

        phones.put("params", paramsList);
        phones.put("phoneNum", "18038102253");

        phonesList.add(phones);
        postJson.put("phones", phonesList);

        System.out.println(postJson);

        String resultInfo = Util.doHttpPostUseHead(url, postJson.toString(), "UTF-8", 120000, 0, headParams);
        System.out.println(resultInfo);

        JSONObject resultJson = JSONObject.fromObject(resultInfo);
        String data = resultJson.getString("code");

        return data;
    }

    public static String sendTrigger5Gmsg(String url, String access_token, String templateId, String serviceId, String mobileNo) throws Exception {

        // 查出传真发送的地址
        Map<String, String> headParams = new HashMap<String, String>();
        headParams.put("Content-Type", "application/json");
        headParams.put("Authorization", "Bearer " + access_token);
        headParams.put("serverRoot", "http://121.37.214.141/sendVpMsg");

        JSONObject postJson = new JSONObject();
        postJson.put("templateId", templateId);
        postJson.put("serviceId", serviceId);

        JSONArray mobileNos = new JSONArray();
        mobileNos.add(mobileNo);
        postJson.put("mobileNos", mobileNos);

        log.debug("5G消息触发-调用地址:" + url + " 调用参数信息:" + postJson);

        String resultInfo = Util.doHttpPostUseHead(url, postJson.toString(), "UTF-8", 120000, 0, headParams);
        log.debug("5G消息触发-调用结果:" + resultInfo);

        JSONObject resultJson = JSONObject.fromObject(resultInfo);
        String data = resultJson.getString("code");
        return data;
    }

    public static String sendDynamicTrigger5Gmsg(String url, String access_token, String templateId, String serviceId, String mobileNo) throws Exception {

        // 查出传真发送的地址
        Map<String, String> headParams = new HashMap<String, String>();
        headParams.put("Content-Type", "application/json");
        headParams.put("Authorization", "Bearer " + access_token);
        headParams.put("serverRoot", serverRoot);

        JSONObject postJson = new JSONObject();
        postJson.put("templateId", templateId);
        postJson.put("serviceId", serviceId);

        JSONArray dynamicInfoArr = new JSONArray();
        JSONObject dynamicInfo = JSONObject.fromObject("{\n" + "            \"mobileNo\":\"" + mobileNo + "\",\n" + "            \"dynamicMultivariable\":[\n" + "                {\n" + "                    \"paramName\":\"param1\",\n" + "                    \"paramValue\":\"11111\"\n" + "                },\n" + "                {\n" + "                    \"paramName\":\"param2\",\n" + "                    \"paramValue\":\"22222\"\n" + "                }\n" + "            ]\n" + "        }");
        dynamicInfoArr.add(dynamicInfo);

        postJson.put("dynamicInfo", dynamicInfoArr);

        log.debug("5G消息动态变量模板触发调用地址:" + url + " 调用参数信息:" + postJson);

        String resultInfo = Util.doHttpPostUseHead(url, postJson.toString(), "UTF-8", 120000, 0, headParams);
        log.debug("5G消息动态变量模板触发调用结果:" + resultInfo);

        JSONObject resultJson = JSONObject.fromObject(resultInfo);
        String data = resultJson.getString("code");

        return data;
    }
}
